home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Tutorial / Cookbook / 03c.hilo_C / hilo.c < prev    next >
C/C++ Source or Header  |  1995-06-12  |  768b  |  32 lines

  1. #include <stdio.h>
  2. #include <libc.h>
  3. extern void srandom();
  4.  
  5. main() {
  6. int theNumber, guess, numGuesses;
  7. char still_playing = 'y';
  8. char still_guessing;
  9.     while (still_playing == 'y') {
  10.         srandom(time(0));
  11.         theNumber = random() % ((100)+1);
  12.         numGuesses = 1;
  13.         still_guessing = 'y';
  14.         while (still_guessing == 'y') {
  15.             printf("Guess a number from 1 to 100: ");
  16.             scanf("%d", &guess);
  17.             if (guess == theNumber) {
  18.                 printf("Correct in %d guesses!\n", numGuesses);
  19.                 still_guessing = 'n';
  20.                 printf("Do you want to play again (y/n)? ");
  21.                 scanf("%1s", &still_playing);
  22.             }
  23.             else if (guess > theNumber)
  24.                 printf("Guess %d is too high\n", numGuesses);
  25.             else if (guess < theNumber)
  26.                 printf("Guess %d is too low\n", numGuesses);
  27.             numGuesses ++;
  28.         }
  29.     }
  30. }
  31.  
  32.